package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsGoodsMapper;
import com.qf.mapper.DtsUserMapper;
import com.qf.pojo.*;
import com.qf.service.*;
import com.qf.vo.GoodsAllinone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Writer;
import java.util.Date;

/**
 * @author wanghaotian
 * time:2022/12/16,0016 10:11
 * description:
 */

@Service
public class DtsGoodsServiceImpl implements DtsGoodsService {

    @Autowired
    private DtsGoodsMapper goodsMapper;

    @Autowired
    private DtsCartService cartService;

    @Autowired
    private DtsOrderGoodsService orderGoodsService;

    @Autowired
    private DtsGoodsAttributeService goodsAttributeService;

    @Autowired
    private DtsGoodsSpecificationService goodsSpecificationService;

    @Autowired
    private DtsGoodsProductService goodsProductService;

    @Override
    public int count() {
        LambdaQueryWrapper<DtsGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsGoods::getDeleted,false);
        return goodsMapper.selectCount(wrapper);
    }

    @Override
    public IPage<DtsGoods> findPage(Integer page, Integer limit, String goodsSn, String name, String sort, String order) {
        IPage<DtsGoods> iPage = new Page<>(page,limit);
        QueryWrapper<DtsGoods> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(goodsSn)) {
            wrapper.like("goodsSn", goodsSn);
        }
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if (order.equalsIgnoreCase("desc")) {
                wrapper.orderByDesc(sort);
            }else {
                wrapper.orderByAsc(sort);
            }
        }
        iPage = goodsMapper.selectPage(iPage, wrapper);
        return iPage;
    }

    @Override
    public DtsGoods selectById(Integer id) {
        DtsGoods goods = goodsMapper.selectById(id);
        return goods;
    }

    @Override
    public void update(GoodsAllinone goodsAllinone) {
        int orderCnt = orderGoodsService.findByGoodsId(goodsAllinone.getGoods().getId());
        int cartCnt = cartService.findByGoodsId(goodsAllinone.getGoods().getId());
        if (orderCnt > 0 || cartCnt > 0) {
            throw new RuntimeException("商品存在订单或购物车中，不能修改");
        }
        goodsMapper.updateById(goodsAllinone.getGoods());
        goodsAttributeService.deleteByGoodsId(goodsAllinone.getGoods().getId());
        for (DtsGoodsAttribute attribute : goodsAllinone.getAttributes()) {
            attribute.setGoodsId(goodsAllinone.getGoods().getId());
            goodsAttributeService.insert(attribute);
        }
        goodsSpecificationService.deleteByGoodsId(goodsAllinone.getGoods().getId());
        for (DtsGoodsSpecification specification : goodsAllinone.getSpecifications()) {
            specification.setGoodsId(goodsAllinone.getGoods().getId());
            goodsSpecificationService.insert(specification);
        }
        goodsProductService.deleteByGoodsId(goodsAllinone.getGoods().getId());
        for (DtsGoodsProduct product : goodsAllinone.getProducts()) {
            product.setGoodsId(goodsAllinone.getGoods().getId());
            goodsProductService.insert(product);
        }
    }

    @Override
    public void delete(DtsGoods goods) {
        int orderCnt = orderGoodsService.findByGoodsId(goods.getId());
        int cartCnt = cartService.findByGoodsId(goods.getId());
        if (orderCnt > 0 || cartCnt > 0) {
            throw new RuntimeException("商品存在订单或购物车中，不能修改");
        }
        goodsAttributeService.deleteByGoodsId(goods.getId());
        goodsSpecificationService.deleteByGoodsId(goods.getId());
        goodsProductService.deleteByGoodsId(goods.getId());
        goodsMapper.deleteById(goods);
    }

    @Override
    public void add(GoodsAllinone goodsAllinone) {
        DtsGoods goods = goodsAllinone.getGoods();
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        LambdaQueryWrapper<DtsGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsGoods::getName,goods.getName());
        Integer count = goodsMapper.selectCount(wrapper);
        if (count > 0) {
            throw new RuntimeException("商品名已存在");
        }
        goods.setAddTime(new Date());
        goods.setUpdateTime(new Date());
        goods.setDeleted(false);
        goodsMapper.insert(goods);
        for (DtsGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goods.getId());
            goodsAttributeService.insert(attribute);
        }
        for (DtsGoodsSpecification specification : specifications) {
            specification.setGoodsId(goods.getId());
            goodsSpecificationService.insert(specification);
        }
        for (DtsGoodsProduct product : products) {
            product.setGoodsId(goods.getId());
            goodsProductService.insert(product);
        }
    }
}
